﻿using CPPEI.Coolzon.Metadata;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.Views
{
    /// <summary>
    /// QueryView.xaml 的交互逻辑
    /// </summary>
    public partial class QueryView : CPPEI.Coolzon.UI.View
    {
        public List<Parameter> Parameters;
        public List<Parameter> Results;
        private Hashtable Elements;
        public QueryView()
        {
            InitializeComponent();
        }

        public QueryView(List<Parameter> parameters)
            : this()
        {
            Parameters = parameters;

            InitControl();
        }

        private void InitControl()
        {
            if (Parameters != null && Parameters.Any())
            {
                Elements = new Hashtable();
                int i = 0;
                foreach (Parameter parameter in Parameters)
                {                    
                    RowDefinition row = new RowDefinition();
                    row.Height = new GridLength(40);
                    gdElements.RowDefinitions.Add(row);
                    TextBlock tb = new TextBlock();
                    tb.Text = parameter.Caption + "：";
                    Grid.SetColumn(tb, 0);
                    Grid.SetRow(tb, i);

                    ValueEditors.ValueEditor element = CreateControl(parameter.ValueType);
                    if (!string.IsNullOrWhiteSpace(parameter.Value))
                    {
                        element.SetValueString(parameter.Value);
                    }
                    Grid.SetColumn(element, 1);
                    Grid.SetRow(element, i);

                    gdElements.Children.Add(tb);
                    gdElements.Children.Add(element);
                    Elements.Add(parameter.Name, element);
                    i++;
                }
            }
            else
            {
                gdElements.RowDefinitions.Add(new RowDefinition());
                TextBlock tbMsg = new TextBlock();
                tbMsg.Text = "没有配置查询参数.";
                Grid.SetColumn(tbMsg, 0);
                Grid.SetRow(tbMsg, 0);
                gdElements.Children.Add(tbMsg);
            }
        }

        private ValueEditors.ValueEditor CreateControl(string valueType)
        {
            return ValueEditors.ValueEditorFactory.Create(valueType);
        }
        private void btnSubmt_Click(object sender, RoutedEventArgs e)
        {
            if (Parameters == null || !Parameters.Any()) return;
            if (Elements != null && Elements.Count > 0)
            {
                Results = new List<Parameter>();
                foreach (DictionaryEntry de in Elements)
                {
                    if (Parameters.Any(p => p.Name.Equals(de.Key)))
                    {
                        Parameter res = Parameters.Where(p => p.Name.Equals(de.Key)).First();
                        res.Value = (de.Value as ValueEditors.ValueEditor).GetValueString();

                        Results.Add(res);
                    }
                }
            }
            Submit();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Cancel();
        }
    }
}
